package cate.common.table.item;

import cate.common.annotation.SplitParse;
import cate.common.table.ERow;
import cate.common.table.XSSFRowWrap;
import cate.common.table.d.GDProp;
import cate.common.util.SplitParser;
import cate.common.util.random.RWItem;
import cate.common.util.random.RWList;

import java.util.List;

public class ItemPackRow extends ERow {

    /** 道具小类 */
    public int type;
    /** 产出配置 */
    public String outputStr;
    /** 种类 */
    public int kind;
    /** 是否公告 */
    public boolean announce;
    /** 公告道具 */
    public String announceItemsStr;
    /** 开启消耗 */
    public String consumeStr;

    @Override
    public void parse(XSSFRowWrap xrw) {
        super.parse(xrw);
        this.id = xrw.getInteger("道具ID");
        this.type = xrw.getInteger("道具小类", 0);
        this.outputStr = xrw.getString("产出配置");
        this.kind = xrw.getInteger("种类", 1);
        this.announce = xrw.getBoolean("是否公告", false);
        this.announceItemsStr = xrw.getString("公告道具");
        this.consumeStr = xrw.getString("开启消耗");
    }

    /** 产出 */
    public PackOutput output = new PackOutput();

    @Override
    public void decode() {
        output.parse(type, outputStr, kind, id);
    }

    public static class PackOutput{
        public int type;
        public int kind;
        // 需要id做特殊处理
        public int id;

        public List<FixItem> fixItemList;
        public List<RandomItem> randomItemList;
        public List<ChooseItem> chooseItemList;
        public List<RandomItem> costItemList;
        public RWList<UncertainItem> uncertainItemList;

        public void parse(int type, String config, int kind, int id) {
            this.type = type;
            this.kind = kind;
            this.id = id;
            switch (type) {
                case GDProp.Type.PACK_FIX:
                    fixItemList = SplitParser.parse(FixItem.class, config);
                    break;
                case GDProp.Type.PACK_RANDOM:
                    randomItemList = SplitParser.parse(RandomItem.class, config);
                    double weightTotal = randomItemList.stream().mapToDouble(item -> item.weight).sum();
                    double weightPos = 0;
                    for(RandomItem item : randomItemList){
                        weightPos += item.weight;
                        item.weight = weightPos / weightTotal;
                    }
                    break;
                case GDProp.Type.PACK_CHOOSE:
                    chooseItemList = SplitParser.parse(ChooseItem.class, config);
                    break;
                case GDProp.Type.PACK_COST_ITEM:
                    costItemList = SplitParser.parse(RandomItem.class, config);
                    weightTotal = costItemList.stream().mapToDouble(item -> item.weight).sum();
                    weightPos = 0;
                    for(RandomItem item : costItemList){
                        weightPos += item.weight;
                        item.weight = weightPos / weightTotal;
                    }
                    break;
                case GDProp.Type.PACK_UNCERTAIN_NUMS:
                    uncertainItemList = SplitParser.unCertainParse(config);
                    break;
                default:
                    break;
            }
        }
    }

    public static class FixItem {
        @SplitParse(order = 0, note = "物品大类")
        public byte type;

        @SplitParse(order = 1, note = "模板ID")
        public int tid;

        @SplitParse(order = 2, defaultValue = "1", note = "数量")
        public int num;
    }

    public static class RandomItem {
        @SplitParse(order = 0, note = "物品大类")
        public byte type;

        @SplitParse(order = 1, note = "模板ID")
        public int tid;

        @SplitParse(order = 2, defaultValue = "1", note = "数量")
        public int num;

        @SplitParse(order = 3, defaultValue = "100", note = "权重")
        public double weight;
    }

    public static class ChooseItem {
        @SplitParse(order = 0, note = "物品大类")
        public byte type;

        @SplitParse(order = 1, note = "模板ID")
        public int tid;

        @SplitParse(order = 2, defaultValue = "1", note = "数量")
        public int num;
    }

    public static class UncertainItem implements RWItem {
        public String res;

        public double weight;

        @Override
        public double getWeight() {
            return weight;
        }
    }
}
